// pages/post-detail/post-detail.js
import { postList } from '../../data/data'
// 获取全局变量
const app = getApp()
Page({

  /**
   * 页面的初始数据
   * 不做数据绑定加上下划线
   * 做数据绑定不加下划线
   */
  data: {
    postData: {},
    _pid: null,
    collected: false,
    isPlaying: false,
    _postsCollected: {},
    _mgr: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      _pid: options.pid
    })
    const postData = postList.find(i => i.postId == this.data._pid)
    let postsCollected = wx.getStorageSync('posts_collected')
    let collected = postsCollected[options.pid]

    if(!postsCollected) {
      postsCollected = {}
      postsCollected[this.data._pid] = false
      wx.setStorageSync('posts_collected', postsCollected)
    }
    else {
      if(!collected){
        postsCollected[this.data._pid] = false
        wx.setStorageSync('posts_collected', postsCollected)
      }
    }
  
    this.setData({
      postData,
      collected,
      postsCollected,
      isPlaying: this.currentMusicIsPlaying()
    })

    const mgr = wx.getBackgroundAudioManager()
    this.data._mgr = mgr
    mgr.onPlay(this.onMusicStart)
    mgr.onPause(this.onMusicStop)
  },
  async onCollection(event){
    // 模态对话框
    const result = await wx.showModal({
      title: '是否收藏文章'
    })
    if(result.confirm) {
      // 动态属性赋值
      // 1、 obj.value
      // 2、obj['value'] 动态性更加好
      let pid = this.data._pid
      this.data.postsCollected[pid] = !this.data.collected
      this.setData({
        collected: !this.data.collected
      })

      // 轻提示
      wx.showToast({
        title: this.data.collected ? '收藏成功' : '取消收藏成功',
        duration: 1000
      })
      wx.setStorageSync('posts_collected', this.data.postsCollected)
    }
   
  },
  onMusicStart(event){
    const mgr = this.data._mgr

    console.log(postList, postList[this.data._pid], this.data._pid)
    const music = postList[this.data._pid].music
    
    console.log(music.url)
    mgr.src = music.url
    mgr.title = music.title
    mgr.coverImgUrl = music.coverImg
    app.gIsPlayingMusic = true
    app.gIsPlayingPostId = this.data._pid
    this.setData({
      isPlaying: true
    })
  },
  onMusicStop(event){
    const mgr = this.data._mgr
    app.gIsPlayingMusic = false
    app.gIsPlayingPostId = -1
    mgr.stop()
    this.setData({
      isPlaying: false
    })
  },
  currentMusicIsPlaying(){
    if(app.gIsPlayingMusic && app.gIsPlayingPostId ===  this.data._pid){
      return true
    }
    return false
  },
  async onShare(event){
    const result = await wx.showActionSheet({
      itemList: ['分享到QQ', '分享到微信', '分享到']  
    })
    console.log(result)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})